package main

import (
	"flag"
	"fmt"
	"gitee.com/jmdvirus/jkgo/jk/jklog"
	"io/ioutil"
	"os"
	"strconv"
)

var (
	mode = flag.Int("mode", 1, "read clipboard/ connect to server and wait")
	addr = flag.String("addr", "0.0.0.0", "addr to connect")
	port = flag.Int("port", 6789, "port to connect")
	data = flag.String("data", "", "file to read, only in mode=0")
	id   = flag.String("id", "clientTest", "client unique")
)

func main() {
	flag.Parse()

	jklog.L().SetLevel(jklog.LEVEL_ALL)
	jklog.L().Infoln("start")

	// read clipboard out
	// connect to server wait data

	rc, err := NewRecvClipboard(*addr, *port)
	if err != nil {
		jklog.L().Errorln(err)
		return
	}
	sdata := "register" + "|" + *id + "\n"
	err = rc.SendData(sdata)
	if err != nil {
		jklog.L().Errorln("send register fail ", err)
		return
	}
	jklog.L().Infoln("connect to server success mode = ", *mode)
	if *mode == 0 {
		f, err := os.OpenFile(*data, os.O_RDONLY, os.ModePerm)
		d := *data
		if err == nil {
			dd, _ := ioutil.ReadFile(*data)
			d = string(dd)
		}
		f.Close()
		sdata = "clip" + "|" + strconv.Itoa(len(d)) + "|" + d
		err = rc.SendData(sdata)
		if err != nil {
			jklog.L().Errorln("send data ", err)
			return
		}
	} else if *mode == 1 {
		go rc.CheckClipboard()
		err = rc.RecvCycle()
		if err != nil {
			jklog.L().Errorln(err)
		}
	}
	jklog.L().Infoln("any string to exit")
	var s int
	fmt.Scan(&s)

	jklog.L().Infoln("end")
}
